package com.dpteam.dao.impl;

import com.dpteam.dao.UserDao;
import com.dpteam.model.User;
import com.dpteam.util.Page;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: luolei
 * Date: 2016/1/7
 * Time: 16:54
 */
@Repository
public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao {

    @Override
    public User findByName(String name) {
        String hql = "from User where name = ?";
        return get(hql, name);
    }

    @Override
    public User findByEmail(String email) {
        String hql = "from User where email = ?";
        return get(hql, email);
    }

    @Override
    public void updateUser(int id, String phone, String email, String newPath) {
        String hql = "update User set phone=?,email=?,imgPath=? where id = ?";
        executeHql(hql,new Object[]{phone,email,newPath,id});
    }
    @Override
    public User findByAccount(String account) {
        String hql = "from User where account = ?";
        return get(hql, account);
    }

    @Override
    public User findByIDCard(String IDCard) {
        String hql = "from User where IDCard = ?";
        return get(hql, IDCard);
    }

    @Override
    public User findByPhone(String phone) {
        String hql = "from User where phone = ?";
        return get(hql, phone);
    }

    @Override
    public List<User> findByGender(int gender) {
        String hql = "from User where gender = ? order by id ";
        return find(hql, gender);
    }

    @Override
    public List<User> findByRoleId(int roleId) {
        String hql = "from User where roleId = ? order by id ";
        return find(hql, roleId);
    }

    @Override
    public List<User> findByGenderByPage(int gender, Page page) {
        String hql = "from User where gender = ? order by id ";
        return find(hql, page, gender);
    }

    @Override
    public List<User> findByRoleIdByPage(int roleId, Page page) {
        String hql = "from User where roleId = ? order by id ";
        return find(hql, page, roleId);
    }

    @Override
    public List<User> findAll() {
        String hql = "from User order by id ";
        return find(hql);
    }

    @Override
    public List<User> findAllByPage(Page page) {
        String hql = "from User order by id ";
        return find(hql, page);
    }

    @Override
    public void deleteById(int id) {
        User user = get(User.class, id);
        if (user != null) {
            user.setRole(null);
            save(user);
        }
    }
}
